﻿using System;
using System.Configuration;
using System.IO;
using OpenSmtp.Mail;

namespace Workflow.Common.mail
{
	public class MailHelper
	{
		/// <summary>
		/// 发送邮件(无附件)
		/// </summary>
		/// <param name="userName"></param>
		/// <param name="passWord"></param>
		/// <param name="from"></param>
		/// <param name="tos"></param>
		/// <param name="ccs"></param>
		/// <param name="subject"></param>
		/// <param name="body"></param>
		/// <returns>是否发送成功</returns>
		public bool SendMail(string userName, string passWord, string from, string[] tos, string[] ccs, string subject, string body)
		{
			return SendMail(userName, passWord, from, tos, ccs, subject, body, null);
		}
		/// <summary>
		/// 发送邮件(带附件)
		/// </summary>
		/// <param name="userName"></param>
		/// <param name="passWord"></param>
		/// <param name="from"></param>
		/// <param name="tos"></param>
		/// <param name="ccs"></param>
		/// <param name="subject"></param>
		/// <param name="body"></param>
		/// <param name="attechments"></param>
		/// <returns>是否发送成功</returns>
		public bool SendMail(string userName, string passWord, string from, string[] tos, string[] ccs, string subject, string body, string[] attechments)
		{
			return SendMail(userName, passWord, from, tos, ccs, null, subject, body, attechments);
		}

		/// <summary>
		/// 发送邮件
		/// </summary>
		/// <param name="userName"></param>
		/// <param name="passWord"></param>
		/// <param name="from"></param>
		/// <param name="tos"></param>
		/// <param name="ccs"></param>
		/// <param name="bccs"></param>
		/// <param name="subject"></param>
		/// <param name="body"></param>
		/// <param name="attechments"></param>
		/// <returns>是否发送成功</returns>
		public bool SendMail(string userName, string passWord, string from, string[] tos, string[] ccs, string[] bccs, string subject, string body, string[] attechments)
		{
			#region 创建要发送的mail
			SmtpConfig.VerifyAddresses = true;
			MailMessage mail = new MailMessage();
			mail.Charset = "utf-8";
			mail.From = new EmailAddress(from);
			if (String.IsNullOrEmpty(userName))
			{
				throw new ArgumentNullException("userName");
			}
			if (String.IsNullOrEmpty(passWord))
			{
				throw new ArgumentNullException("passWord");
			}
			if (String.IsNullOrEmpty(from))
			{
				throw new ArgumentNullException("from");
			}
			if (tos == null || tos.Length == 0)
			{
				throw new ArgumentNullException("tos");
			}
			foreach (string to in tos)
			{
				if (!String.IsNullOrEmpty(to) && to.Trim().Length > 0)
				{
					//if (to.IndexOf("@merck.com") >= 0)
					//{
						mail.To.Add(new EmailAddress(to.Trim()));
					//}
				}
			}
			if (ccs != null)
			{
				foreach (string cc in ccs)
				{
					if (!String.IsNullOrEmpty(cc) && cc.Trim().Length > 0)
					{
						mail.CC.Add(new EmailAddress(cc.Trim()));
					}
				}
			}
			if (bccs != null)
			{
				foreach (string bcc in bccs)
				{
					if (!String.IsNullOrEmpty(bcc) && bcc.Trim().Length > 0)
					{
						mail.BCC.Add(new EmailAddress(bcc.Trim()));
					}
				}
			}
			mail.Subject = subject ?? String.Empty;
			mail.HtmlBody = body ?? String.Empty;
			if (attechments != null)
			{
				foreach (string attechment in attechments)
				{
					if (File.Exists(attechment))
					{
						mail.Attachments.Add(new Attachment(attechment));
					}
				}
			}
			#endregion

	
			#region 发送邮件
			try
			{

				string post =ConfigurationManager.AppSettings["mail.host"];
				int host = int.Parse(ConfigurationManager.AppSettings["mail.post"]);
				Smtp smtp = new Smtp(post, host);
				
				smtp.Username = userName;
				smtp.Password = passWord;
				smtp.SendTimeout = 1000 * 300; //(300 seconds)
				try
				{
					smtp.SendMail(mail);
					return true;
				}
			
				catch (Exception ex)
				{
					//throw new ExceptionBusiness(ex.Message);
					throw ex;
				}


			}
			catch (Exception ex)
			{
                throw ex;
			}
			#endregion

		}
	}
}
